CC = gcc
CFLAGS= -Wall \
		-fpic \
		-g \
		-coverage \
		-lm \
		-Wshadow \
		-Wunreachable-code \
		-Wredundant-decls \
		-Wmissing-declarations \
		-Wmissing-prototypes \
		-Wold-style-definition \
		-DNDEBUG \
		-std=gnu99
testnums=1 2 3 4
unittests=$(addprefix unittest, $(testnums))
cardtests=randomtestadventurer randomtestcard

rngs.o: rngs.h rngs.c
	$(CC) -c rngs.c $(CFLAGS)

dominion.o: dominion.h dominion.c rngs.o 
	$(CC) -c dominion.c  $(CFLAGS)

playdom: dominion.o playdom.c
	$(CC) -o playdom playdom.c dominion.o rngs.o $(CFLAGS)

testDrawCard: testDrawCard.c dominion.o rngs.o
	$(CC) -o testDrawCard  testDrawCard.c dominion.o rngs.o $(CFLAGS)

badTestDrawCard: badTestDrawCard.c dominion.o rngs.o
	$(CC) -o badTestDrawCard  badTestDrawCard.c dominion.o rngs.o $(CFLAGS)

testBuyCard: testDrawCard.c dominion.o rngs.o
	$(CC) -o testDrawCard  testDrawCard.c dominion.o rngs.o $(CFLAGS)

testAll: dominion.o testSuite.c
	$(CC) -o testSuite testSuite.c  dominion.o rngs.o $(CFLAGS)

interface.o: interface.h interface.c
	$(CC) -c interface.c  $(CFLAGS)

runtests: testDrawCard
	./testDrawCard &> unittestresult.out
	gcov dominion.c >> unittestresult.out
	cat dominion.c.gcov >> unittestresult.out

player: player.c interface.o
	$(CC) -o player player.c  dominion.o rngs.o interface.o $(CFLAGS)

unittest%: dominion.o rngs.o interface.o
	$(CC) -o $@ $@.c $^ $(CFLAGS)

cardtest%: dominion.o rngs.o interface.o
	$(CC) -o $@ $@.c $^ $(CFLAGS)

randomtestcard: dominion.o rngs.o interface.o
	$(CC) -o $@ $@.c $^ $(CFLAGS)

randomtestadventurer: dominion.o rngs.o interface.o
	$(CC) -o $@ $@.c $^ $(CFLAGS)

runUnitTests: $(unittests) 
	-@for t in $(unittests); do 						\
		./$$t >> unittestresult.out;					\
		gcov -bf dominion.c >> unittestresult.out;		\
		cat dominion.c.gcov >> unittestresult.out;		\
	done
	
	-@cat unittestresult.out | ./unique_fail.pl > unittestfail.out

runRandomTests: randomtestcard randomtestadventurer
	-@for t in $^; do 									\
		./$$t >> randomtestresult.out;					\
		gcov -bf dominion.c >> randomtestresult.out;	\
		cat dominion.c.gcov >> randomtestresult.out;	\
	done
	-@cat randomtestresult.out | ./unique_fail.pl > randomtestfail.out

runAllTests: runUnitTests runRandomTests

all: playdom player testDrawCard testBuyCard badTestDrawCard

clean:
	-@rm -f *.o $(unittests) $(cardtests) testDrawCard badTestDrawCard testBuyCard playdom.exe playdom test.exe test player player.exe testInit testInit.exe *.gcov *.gcda *.gcno *.so
